package com.example.tests;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.List;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import com.example.fw.ContactObject;
import com.example.fw.Groups;



public class ContactCreationTests extends TestBase{
		
	@Test(dataProvider = "contactsFromFile", dataProviderClass = ContactDataGenerator.class)
	public void testValidContactCanBeCreated(ContactObject validContact) throws Exception {
		
			/*Set<ContactObject> oldContactList = app.getContactHelper().getContacts();
			app.getContactHelper().createContact(validContact);
			Set<ContactObject> newContactList = app.getContactHelper().getContacts();
			verifyContactAdded(oldContactList, validContact, newContactList);*/
		List<ContactObject> oldContactList = app.getHibernateHelper().getContacts();
		app.getContactHelper().createContact(validContact);
		List<ContactObject> newContactList = app.getHibernateHelper().getContacts();
		verifyContactAdded(oldContactList, validContact, newContactList);

	}

	private void verifyContactAdded(List<ContactObject> oldContactList, ContactObject validContact
			, List<ContactObject> newContactList) {
		
		//validContact.setFirstname(String.format("%s %s", validContact.firstname, validContact.lastname));
		oldContactList.add(validContact);
		/*Boolean isFound = false;
		for(ContactObject currentContact : newContactList)
		{
			if(currentContact.equals(validContact)) 
			{
				isFound = true;
				break;
			}
		}*/
		assertThat(newContactList, equalTo(oldContactList));
		//Assert.assertFalse(String.format("Contact [%s] didn't found", validContact.firstname), !isFound);
	}
	
	
	@AfterMethod
	public void testUIConformsWithDB(){
		if (app.getProperty("uiDb.check") != null){
			Groups dbList = app.getHibernateHelper().getGroups();
			Groups uiList = app.getGroupHelper().getUiGroups();
			assertThat(uiList, equalTo(dbList));
		}
		
	}
	
		
}
